<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="entidadesVO.ArticuloVO"%>
<%@ page import="facade.Facade"%>
<%@ page import="java.util.*" %>
<%@ page import="javax.naming.*"%>
<%@ page import="utils.ZaraException"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>ZARA - La Coruna</title>
	<link media="screen" href="estilo.css" type="text/css" rel="stylesheet"></link>
</head>
<body>
	<ul>
		<li><a class="actual" href="ofad.html">OFAD</a></li>
		<li><a href="artHogar.html">Articulos Hogar</a></li>
		<li><a href="artRopa.html">Articulos Ropa</a></li>
		<li><a href="solDist.html">Sol. Dist.</a></li>
	</ul>
	<div id="contenido" align="center">
	<form action="AddArticuloOFADSrvl" method="post">
		<h2><%=(request.getParameter("modo").equals("B")?"Articulo Buscado":(request.getParameter("modo").equals("LT")?"Listado De Articulos Tentativos":""))%></h2>	
		<div class="caja">
		<%
		Facade modelo = null;
		Hashtable props = new Hashtable();
		props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
		String naming = "LaCorunaWebServer/FacadeBean/remote";
		try {			
			InitialContext ic = new InitialContext(props);
			modelo = (Facade)ic.lookup(naming);			
		} catch (Exception e) {		
			e.printStackTrace();%>
			<div><span class="error">No se pudo conectar al servidor</span></div>
		<%}		
		if(modelo != null){		
		%>
			<table width="100%">
				<tr>
					<td class="cabecera">Referencia</td>
					<td class="cabecera">Descripcion</td>
					<td class="cabecera">Precio U.</td>
					<td class="cabecera">Descuento %</td>
					<td class="cabecera">Nuevo</td>
					<td class="cabecera">Detalle</td>
					<td class="cabecera"></td>
				</tr>
		<%	if(request.getParameter("modo").equals("B")){
				ArticuloVO art = null;
				try{
					art = modelo.getArticulo(request.getParameter("artId"));
				}catch(ZaraException ze){%>
					<tr><td colspan="8"><span class="error"><%= ze.getMessage() %></span></td></tr>
				<%}
				if(art!=null){
					HttpSession ses = request.getSession(true);
					ArticuloVO[] artsVO = (ArticuloVO[])ses.getAttribute("articulos");
					boolean found = false;
					if(artsVO != null){
						for(int i=0; i < artsVO.length; i++){
							if(art.getReferencia().equals(artsVO[i].getReferencia())){
								found = true;
								break;
							}
						}
					}%>
				<tr>
					<td class="dato"><%= art.getReferencia()%></td>
					<td class="dato"><%= art.getDescripcion()%></td>
					<td class="dato"><%= art.getPrecio()%></td>
					<td class="dato"><%= art.getDescuento()%></td>
					<td class="dato"><%= (art.getNuevo()==0?"NO":"SI")%></td>
					<td class="dato">
						<a href="detalleArticulo.html">Detalle</a>
					</td>
					<td class="dato">
						<input name="refArt" type="checkbox" value="<%= art.getReferencia()%>" <%=(found ?"checked='checked'":"")%>>
					</td>
				</tr>
				<%}
			}else if(request.getParameter("modo").equals("LT")){
				Object[] arts = null;
				try{
					arts = modelo.getListadoArticuloVOTentativoOFAD().toArray();					
				}catch(ZaraException ze){%>
					<tr><td colspan="8"><span class="error"><%= ze.getMessage() %></span></td></tr>
				<%}
				if(arts != null){
					HttpSession ses = request.getSession(true);
					ArticuloVO[] artsVO = (ArticuloVO[])ses.getAttribute("articulos");
					boolean found = false;
					for (int i = 0; i < arts.length; i++){
						ArticuloVO art = (ArticuloVO) arts[i];
						found = false;
						if(artsVO != null){
							for(int j=0; j < artsVO.length; j++){
							if(art.getReferencia().equals(artsVO[j].getReferencia())){
								found = true;
								break;
							}
						}
					}%>
						<tr>
							<td class="dato"><%= art.getReferencia()%></td>
							<td class="dato"><%= art.getDescripcion()%></td>
							<td class="dato"><%= art.getPrecio()%></td>
							<td class="dato"><%= art.getDescuento()%></td>
							<td class="dato"><%= (art.getNuevo()==0?"NO":"SI")%></td>
							<td class="dato">
								<a href="detalleArticulo.html">Detalle</a>
							</td>
							<td class="dato">
								<input name="refArt" type="checkbox" value="<%= art.getReferencia()%>" <%=(found?" checked='checked'":"")%>>
							</td>
						</tr>					
					<%}
				}
			}%>
			</table>
		<%}%>		
		</div>
		<div style="width: 80%">
			<hr>
		</div>	
		<div style="width: 80%">
			<table width="100%">
				<tr>
					<td align="right">
						<input type="submit" value="Agregar">
					</td>
					<td align="left">
						<input type="button" value="Volver" onclick="document.location = 'ofad.jsp';">
					</td>
				</tr>						
			</table>
		</div>
	</form>
	</div>
</body>
</html>